﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaptchaMVC.Attribute;
using System.Web.Security;
using System.Web.Profile;
using TenderSampleApp.Models;
using System.Net.Mail;
using System.Net;

namespace TenderSampleApp.Areas.Administrator.Controllers
{
    public class RegisterController : Controller
    {
        //
        // GET: /Administrator/Register/
        [Authorize(Roles = "ADMIN ")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "ADMIN ")]
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [Authorize(Roles = "ADMIN ")]
        public ActionResult Register(RegisterModel model, string typeOfPerson)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;

                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
                TempData["Message"] = "Капча не вірна4";
                if (createStatus == MembershipCreateStatus.Success)
                {
                    Roles.AddUserToRole(model.UserName, typeOfPerson);
                    //FormsAuthentication.SetAuthCookie(model.UserName,false /* createPersistentCookie */);


                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                    {
                        Credentials = new NetworkCredential("responcestateproperty@gmail.com", "sliva051"),
                        EnableSsl = true
                    };
                    MailMessage mesage = new MailMessage();

                    mesage.From = new MailAddress("responcestateproperty@gmail.com");
                    mesage.To.Add(model.Email);
                    mesage.Subject = "Регістрація на сайті";


                    mesage.Body = @"Для того щоб активувати аккаунт вам необхідно перейти по ссилці http://" + Request.Url.Authority + "/Account/LogOn";
                    client.Send(mesage);

                    return View("RegisterSuccess");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }


        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion

    }
}
